//
//  WSPayController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/14.
//

#import "WSPayController.h"
#import "WSRechargeCell.h"
#import "WSPurchaseRequest.h"
#import "AppDelegate.h"
#import "WSPurchaseManager.h"
#import "WSGoods.h"
#import "UIButton+ImageAlign.h"
#import "WSPayRequest.h"

@interface WSPayController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UILabel *coinsLabel;
@property (weak, nonatomic) IBOutlet UIButton *autoUnlockBtn;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@end

@implementation WSPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutMainView];
    [self layourAutoUnlockBtn];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mainViewBottom.constant = 0;
        [self.view uploadLayout];
    });
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:WSCoinsChangeNotification object:nil];
}

- (void)layoutMainView {
    self.coinsLabel.text = WSSF(@"%@：%ld", APPLocalized(@"剧豆餘額"),(long)appUserCenter.coins);
    self.priceLabel.text = WSSF(@"%ld剧豆", self.charonTV_video.coins);
}

- (void)layourAutoUnlockBtn {
    [self.autoUnlockBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleLeft) padding:7.5];
    self.autoUnlockBtn.selected = appConfig.isAutoPay;
}

- (void)payVideo {
    [WSPayRequest payWithChapter_id:self.charonTV_video.id success:^(NSString *string) {
        self.charonTV_video.url = string;
        if ([self.delegate respondsToSelector:@selector(coinsPayControllerReloadAction:)]) {
            [self.delegate coinsPayControllerReloadAction:self.charonTV_video];
        }
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}
    
- (IBAction)payAction:(id)sender {
    [self payVideo];
}

- (IBAction)moreAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(coinsPayControllerMoreAction)]) {
        [self.delegate coinsPayControllerMoreAction];
    }
}

- (IBAction)autoAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.isAutoPay = sender.selected;
}


@end
